package com.jamal2362.apktool;

import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import androidx.appcompat.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
import com.jamal2362.apktool.preference.CustomHeaderAdapter;
import com.jamal2362.apktool.util.Settings;

public class SettingActivity extends PreferenceActivity
{
	private List<PreferenceActivity.Header> mHeaders = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Settings.loadSettings(this);
		setTheme();
		super.onCreate(savedInstanceState);
	}

	@Override
    protected void onPostCreate(Bundle savedInstanceState)
	{
        super.onPostCreate(savedInstanceState);
        LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
        Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
        root.addView(bar, 0);
        bar.setNavigationOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v)
				{
					finish();
				}
			});
    }

	@Override
	public void onBuildHeaders(List<PreferenceActivity.Header> target)
	{
		mHeaders = target;
		loadHeadersFromResource(R.xml.preference, target);
	}

	@Override
	protected boolean isValidFragment(String fragmentName)
	{
		return true;
	}

	@Override
	public void onHeaderClick(PreferenceActivity.Header header, int position)
	{
		super.onHeaderClick(header, position);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		setTitle(getString(R.string.settings));
	}

	@Override
	public void setListAdapter(ListAdapter adapter)
	{
		int i, count;
		if (mHeaders == null)
		{
			mHeaders = new ArrayList<PreferenceActivity.Header>();
			count = adapter.getCount();
			for (i = 0; i < count; ++i)
			{
				mHeaders.add((PreferenceActivity.Header) adapter.getItem(i));
			}
		}
		CustomHeaderAdapter customAdapter = new CustomHeaderAdapter(this, mHeaders, R.layout.preference_header_item, true);
		super.setListAdapter(customAdapter);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle state)
	{
		super.onRestoreInstanceState(state);
	}

	private void setTheme()
	{
		boolean lightTheme = Settings.getb(this, "light_theme", true);
		int flag =  View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
		if (lightTheme)
		{
			setTheme(R.style.AppThemeLight);
			View view = getWindow().getDecorView();
			int flags = view.getSystemUiVisibility() | flag;
			view.setSystemUiVisibility(flags);
		}
		else
		{
			setTheme(R.style.AppTheme);
			if (Build.VERSION.SDK_INT >= 23)
			{
				View view = getWindow().getDecorView();
				int flags = view.getSystemUiVisibility() & (~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) | flag;
				view.setSystemUiVisibility(flags);
			}
		}
	}
}
